package com.aijk.commons.idworker;

import com.aijk.commons.register.ClusterHostUtils;
import com.aijk.commons.register.HostInfo;


/**
 * <P> 分布式ID生成器采用枚举单例 </P>
 * <P> 基于 Twitter Snowflake 算法 </P>
 * Created by ChenDeWei on 2016/7/7.
 */
public enum IdGenerator {

    INSTANCE;

    private IdWorker idWorker;
    boolean init = false;

    IdGenerator() {

        HostInfo hostInfo = ClusterHostUtils.INSTANCE.registerHost();

        idWorker = new IdWorker(hostInfo.getId(), 0);

        init = true;
    }

    public synchronized long nextId() {

        return idWorker.nextId();
    }
}
